/**
 * FileName: SafeDao.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-10 下午4:18:10
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-10 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.data;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.me.safebox.service.DecryFuture;
import com.me.safebox.service.EncryFuture;
import com.me.safebox.service.FileConfig;
import com.me.safebox.service.IFuture;
import com.me.safebox.ui.MainApp;
import com.me.safebox.utils.MyDbHelper;

/**
 * @ClassName: SafeDao
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-12-10 下午4:18:10
 * 
 */
public class SafeboxDao {
	private static final String TAG = "SafeboxDao";

	// private MyDbHelper helper = new MyDbHelper(MainApp.context);

	/**
	 * 
	 * @param sourcePath
	 *            源文件地址
	 * @param desPath
	 *            目标存储地址
	 * @param fileType
	 *            文件类型
	 * @param createDate
	 * @param albumId
	 */
	public void singleInsertEncryFileInfo(String sourcePath, String desPath, int fileType, long createDate, int albumId) {
		MyDbHelper helper = new MyDbHelper();
		ContentValues contentValues = new ContentValues();
		contentValues.put(SqlUtil.COLUMN_SOURCE_PATH, sourcePath);
		contentValues.put(SqlUtil.COLUMN_DES_PATH, desPath);
		contentValues.put(SqlUtil.COLUMN_FILE_TYPE, fileType);
		contentValues.put(SqlUtil.COLUMN_CREATE_DATA, createDate);
		contentValues.put(SqlUtil.COLUMN_FOLDER_FK, albumId);
		helper.insert(contentValues, SqlUtil.SAFE_FILE_TABLE_NAME);
	}

	public void batchDeleteDecryFileInfo(List<Future<IFuture>> resultList) throws InterruptedException, ExecutionException {
		MyDbHelper helper = new MyDbHelper();
		SQLiteDatabase db = helper.getWritableDatabase();
		db.beginTransaction();
		DecryFuture decryFuture = null;
		for (Future<IFuture> df : resultList) {
			decryFuture = (DecryFuture) df.get();
			if (decryFuture.deletePermisson) {
				helper.del(SqlUtil.SAFE_FILE_TABLE_NAME, decryFuture.fileId);
			}
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	/**
	 * 批量插入加密文件信息
	 * 
	 * @param list
	 */
	public void batchInsertEncryFileInfo(List<Future<IFuture>> resultList) throws InterruptedException, ExecutionException {
		MyDbHelper helper = new MyDbHelper();
		SQLiteDatabase db = helper.getWritableDatabase();
		db.beginTransaction();
		EncryFuture encryFuture = null;
		ContentValues contentValues = null;
		for (Future<IFuture> ef : resultList) {
			encryFuture = (EncryFuture) ef.get();
			contentValues = new ContentValues();
			contentValues.put(SqlUtil.COLUMN_SOURCE_PATH, encryFuture.sourceDir);
			contentValues.put(SqlUtil.COLUMN_DES_PATH, encryFuture.desPath);
			contentValues.put(SqlUtil.COLUMN_FILE_TYPE, encryFuture.fileType);
			contentValues.put(SqlUtil.COLUMN_CREATE_DATA, System.currentTimeMillis());
			contentValues.put(SqlUtil.COLUMN_FOLDER_FK, encryFuture.folderId);
			contentValues.put(SqlUtil.COLUMN_FILE_NAME, encryFuture.sourceName);
			db.insert(SqlUtil.SAFE_FILE_TABLE_NAME, null, contentValues);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	/**
	 * 新建一个私密相册
	 * 
	 * @param lable
	 */
	public void createAlbumFolder(String lable) {
		MyDbHelper helper = new MyDbHelper();
		ContentValues contentValues = new ContentValues();
		contentValues.put(SqlUtil.COLUMN_FOLDER_TYPE, FileConfig.FOLDER_TYPE_ALBUM);
		contentValues.put(SqlUtil.COLUMN_FOLDER_LABLE, lable);
		contentValues.put(SqlUtil.COLUMN_CREATE_DATA, System.currentTimeMillis());
		helper.insert(contentValues, SqlUtil.SAFE_FOLDER_TABLE_NAME);
	}

	public List<SafeFolder> getSafeFolder(int folderType) {
		MyDbHelper helper = new MyDbHelper();
		List<SafeFolder> list = new ArrayList<SafeFolder>();
		StringBuffer sb = new StringBuffer();
		sb.append("select * from ");
		sb.append(SqlUtil.SAFE_FOLDER_TABLE_NAME);
		sb.append(" where ");
		sb.append(SqlUtil.COLUMN_FOLDER_TYPE);
		sb.append("=");
		sb.append(folderType);
		Cursor cursor = helper.query(sb.toString(), null);
		SafeFolder defaultFolder = new SafeFolder();// 默认文件夹
		defaultFolder.id = 0;
		defaultFolder.lable = "默认";
		defaultFolder.type = folderType;
		defaultFolder = getFile(defaultFolder, folderType, defaultFolder.id);
		list.add(defaultFolder);
		while (cursor.moveToNext()) {
			SafeFolder folder = new SafeFolder();
			folder.id = cursor.getLong(0);
			folder.lable = cursor.getString(1);
			folder.type = cursor.getInt(2);
			folder.data = cursor.getLong(3);
			folder = getFile(folder, folderType, defaultFolder.id);
			list.add(folder);
		}
		cursor.close();
		helper.close();
		return list;
	}

	public List<SafeFile> getSafeFile(long folderId, int fileType) {
		MyDbHelper helper = new MyDbHelper();
		Cursor cursor = helper.query("select * from " + SqlUtil.SAFE_FILE_TABLE_NAME + " where " + SqlUtil.COLUMN_FILE_TYPE + "=" + fileType + " and " + SqlUtil.COLUMN_FOLDER_FK + "=" + folderId,
				null);
		List<SafeFile> list = new ArrayList<SafeFile>();
		while (cursor.moveToNext()) {
			SafeFile safeFile = new SafeFile();
			safeFile.id = cursor.getLong(0);
			safeFile.fileType = cursor.getInt(1);
			safeFile.sourceDir = cursor.getString(2);
			safeFile.encrptFilePath = cursor.getString(3);
			safeFile.sourceName = cursor.getString(4);
			list.add(safeFile);
		}
		cursor.close();
		helper.close();
		return list;
	}

	private SafeFolder getFile(SafeFolder safeFolder, int fileType, long folderId) {
		MyDbHelper helper = new MyDbHelper();
		Cursor cursor = helper.query("select count(*) , " + SqlUtil.COLUMN_DES_PATH + "," + SqlUtil.COLUMN_FILE_NAME + " from " + SqlUtil.SAFE_FILE_TABLE_NAME + " where " + SqlUtil.COLUMN_FILE_TYPE
				+ "=" + fileType + " and " + SqlUtil.COLUMN_FOLDER_FK + "=" + folderId, null);
		while (cursor.moveToNext()) {
			safeFolder.fileTotal = cursor.getInt(0);
			safeFolder.firstEncrptFilePath = cursor.getString(1);
			safeFolder.firstSourceName = cursor.getString(2);
		}
		cursor.close();
		helper.close();
		return safeFolder;
	}

}
